from django.contrib import admin
from reversion.admin import VersionAdmin
from gxjg.models import *
from gxjg.resources import *
from import_export.admin import ImportExportModelAdmin, ExportActionModelAdmin, ExportActionMixin


class Gxjg0301Inline(admin.StackedInline):
    model = Gxjg0301
    extra = 0
    fieldsets = (
        ('职务信息', {"fields": ["teacher", ("rzbm", "rzks"), ("zwmc", "zwmcm"), "zwpx", ("zwlbm", "zwflm", "zwjbm"), "zghcsdgz", 'dqrzzt']}),
        ("任免信息", {
            'classes': ('collapse',),
            'fields': [("rzrq", "rzfsm"), ("rzyy", "rzwh"), "rzpzdw", "zwbdlbm", ("mzrq", "mzfsm"), ("mzyym", "mzwh"), "mzpzdw"]
        }),
        ("其他信息", {
            "classes": ('collapse',),
            "fields": ['source', 'priority', 'lock', 'remarks', 'create_time', 'update_time']
        }),
    )
    readonly_fields = ['create_time', 'update_time']

    readonly_fields = ['create_time', 'update_time']


@admin.register(Gxjg0101)
class Gxjg0101Admin(VersionAdmin, ImportExportModelAdmin):
    class Media:
        js = ('admin/js/custom.js', )

    change_list_template = "admin/change_list_recover.html"

    list_display = ['gh', 'xm', 'dwh_label', 'sex', 'sfzjh', 'zgxl', 'jzglbm',  'type', 'yrxs', 'dqztm', 'update_time','openoa']
    list_filter = ['gh', 'xbm', 'dwh', 'jzglbm', 'zgxl', 'zgxw', 'zc', 'zcjb', 'sfzjlxm', 'dqztm', 'type', 'yrxs', 'source']
    search_fields = ['gh', 'dwh__label', 'xm', 'sfzjh']
    ordering = ('-update_time', )
    save_on_top = True

    fieldsets = (
        ('常用信息', {'fields': ['gh', 'xm', 'xbm', 'gkdwh', 'dwh', 'dwh_1', 'dwh_2', 'dwh_3', 'dwh_4', 'dwh_5', ('sfzjlxm', 'sfzjh', 'sfzjyxq'), 'jzglbm', 'dqztm', 'type', 'zc', 'zcjb', 'zj', 'yrxs','openoa']}),
        ('基本信息', {
            "classes": ('collapse',),
            'fields': ['ywxm', 'xmpy', 'cym', ('csrq', 'csd'), 'jg', 'mzm', ('gatqwm', 'gjdqm'), 'hyzkm', 'zzmm', 'jkzkm', 'xyzjm', 'xxm', 'zp']
        }),
        ('工作信息', {
            "classes": ('collapse',),
            'fields': ['xqm', ('zgxl', 'zgxw'), 'cjgzny', ('lxrq', 'qxrq'), 'cjny', 'bzlbm', ('dabh', 'dawb'), 'tc', ('xklbm', 'yjxkm', 'ejxkm'), 'yjfx']
        }),
        ("其他信息", {
            "classes": ('collapse',),
            "fields": ['remarks', "source", "priority", "lock", 'create_time', 'update_time']
        })

    )

    resource_class = Gxjg0101Resource
    inlines = [Gxjg0301Inline]
    readonly_fields = ['create_time', 'update_time']

    def save_model(self, request, obj, form, change):
        super(Gxjg0101Admin, self).save_model(request, obj, form, change)

    def delete_model(self, request, obj):
        super(Gxjg0101Admin, self).delete_model(request, obj)

    def delete_queryset(self, request, queryset):
        """Given a queryset, delete it from the database."""
        super(Gxjg0101Admin, self).delete_queryset(request, queryset)

    def sex(self, obj):
        return obj.xbm.label
    sex.short_description = '性别'

    def dwh_label(self, obj):
        return obj.dwh.label if obj.dwh else None

    dwh_label.short_description = '单位'
